package com.example.learn

import java.util.*

fun main() {
    val stackToQueue = StackToQueue<Int>()
    stackToQueue.push(1)
    stackToQueue.push(2)
    stackToQueue.push(3)
    while (!stackToQueue.empty()) {
        val element = stackToQueue.pop()
        print("->$element")
    }
}

class StackToQueue<T> {
    private val stack1 = Stack<T>()
    private val stack2 = Stack<T>()
    private var count = 0
    fun push(element: T) {
        stack1.push(element)
        ++ count
    }

    fun pop(): T? {
        var element: T? = null
        if (stack2.empty()) {
            if (!stack1.empty()) {
                while (!stack1.empty()) {
                    stack2.push(stack1.pop())
                }
                element = stack2.pop()
            }
        } else {
            element = stack2.pop()
        }
        -- count
        return element
    }

    fun empty(): Boolean {
        return count == 0
    }

    fun size(): Int {
        return count
    }
}